{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# happybank——数据探索&特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Gender</th>\n",
       "      <th>City</th>\n",
       "      <th>Monthly_Income</th>\n",
       "      <th>DOB</th>\n",
       "      <th>Lead_Creation_Date</th>\n",
       "      <th>Loan_Amount_Applied</th>\n",
       "      <th>Loan_Tenure_Applied</th>\n",
       "      <th>Existing_EMI</th>\n",
       "      <th>Employer_Name</th>\n",
       "      <th>...</th>\n",
       "      <th>Interest_Rate</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Filled_Form</th>\n",
       "      <th>Device_Type</th>\n",
       "      <th>Var2</th>\n",
       "      <th>Source</th>\n",
       "      <th>Var4</th>\n",
       "      <th>LoggedIn</th>\n",
       "      <th>Disbursed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ID000002C20</td>\n",
       "      <td>Female</td>\n",
       "      <td>Delhi</td>\n",
       "      <td>20000</td>\n",
       "      <td>23-May-78</td>\n",
       "      <td>15-May-15</td>\n",
       "      <td>300000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CYBOSOL</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ID000004E40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Mumbai</td>\n",
       "      <td>35000</td>\n",
       "      <td>07-Oct-85</td>\n",
       "      <td>04-May-15</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>TATA CONSULTANCY SERVICES LTD (TCS)</td>\n",
       "      <td>...</td>\n",
       "      <td>13.25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6762.9</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ID000007H20</td>\n",
       "      <td>Male</td>\n",
       "      <td>Panchkula</td>\n",
       "      <td>22500</td>\n",
       "      <td>10-Oct-81</td>\n",
       "      <td>19-May-15</td>\n",
       "      <td>600000.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>ALCHEMIST HOSPITALS LTD</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ID000008I30</td>\n",
       "      <td>Male</td>\n",
       "      <td>Saharsa</td>\n",
       "      <td>35000</td>\n",
       "      <td>30-Nov-87</td>\n",
       "      <td>09-May-15</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>BIHAR GOVERNMENT</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ID000009J40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Bengaluru</td>\n",
       "      <td>100000</td>\n",
       "      <td>17-Feb-84</td>\n",
       "      <td>20-May-15</td>\n",
       "      <td>500000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>GLOBAL EDGE SOFTWARE</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S134</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ID  Gender       City  Monthly_Income        DOB  \\\n",
       "0  ID000002C20  Female      Delhi           20000  23-May-78   \n",
       "1  ID000004E40    Male     Mumbai           35000  07-Oct-85   \n",
       "2  ID000007H20    Male  Panchkula           22500  10-Oct-81   \n",
       "3  ID000008I30    Male    Saharsa           35000  30-Nov-87   \n",
       "4  ID000009J40    Male  Bengaluru          100000  17-Feb-84   \n",
       "\n",
       "  Lead_Creation_Date  Loan_Amount_Applied  Loan_Tenure_Applied  Existing_EMI  \\\n",
       "0          15-May-15             300000.0                  5.0           0.0   \n",
       "1          04-May-15             200000.0                  2.0           0.0   \n",
       "2          19-May-15             600000.0                  4.0           0.0   \n",
       "3          09-May-15            1000000.0                  5.0           0.0   \n",
       "4          20-May-15             500000.0                  2.0       25000.0   \n",
       "\n",
       "                         Employer_Name  ... Interest_Rate Processing_Fee  \\\n",
       "0                              CYBOSOL  ...           NaN            NaN   \n",
       "1  TATA CONSULTANCY SERVICES LTD (TCS)  ...         13.25            NaN   \n",
       "2              ALCHEMIST HOSPITALS LTD  ...           NaN            NaN   \n",
       "3                     BIHAR GOVERNMENT  ...           NaN            NaN   \n",
       "4                 GLOBAL EDGE SOFTWARE  ...           NaN            NaN   \n",
       "\n",
       "   EMI_Loan_Submitted Filled_Form  Device_Type  Var2  Source  Var4  LoggedIn  \\\n",
       "0                 NaN           N  Web-browser     G    S122     1         0   \n",
       "1              6762.9           N  Web-browser     G    S122     3         0   \n",
       "2                 NaN           N  Web-browser     B    S143     1         0   \n",
       "3                 NaN           N  Web-browser     B    S143     3         0   \n",
       "4                 NaN           N  Web-browser     B    S134     3         1   \n",
       "\n",
       "  Disbursed  \n",
       "0         0  \n",
       "1         0  \n",
       "2         0  \n",
       "3         0  \n",
       "4         0  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据\n",
    "# path to where the data lies\n",
    "\n",
    "train = pd.read_csv(\"./train.csv\",encoding='ISO-8859-1')\n",
    "train.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 37717 entries, 0 to 37716\n",
      "Data columns (total 24 columns):\n",
      " #   Column                 Non-Null Count  Dtype  \n",
      "---  ------                 --------------  -----  \n",
      " 0   ID                     37717 non-null  object \n",
      " 1   Gender                 37717 non-null  object \n",
      " 2   City                   37319 non-null  object \n",
      " 3   Monthly_Income         37717 non-null  int64  \n",
      " 4   DOB                    37717 non-null  object \n",
      " 5   Lead_Creation_Date     37717 non-null  object \n",
      " 6   Loan_Amount_Applied    37677 non-null  float64\n",
      " 7   Loan_Tenure_Applied    37677 non-null  float64\n",
      " 8   Existing_EMI           37677 non-null  float64\n",
      " 9   Employer_Name          37675 non-null  object \n",
      " 10  Salary_Account         32680 non-null  object \n",
      " 11  Mobile_Verified        37717 non-null  object \n",
      " 12  Var5                   37717 non-null  int64  \n",
      " 13  Var1                   37717 non-null  object \n",
      " 14  Loan_Amount_Submitted  22795 non-null  float64\n",
      " 15  Loan_Tenure_Submitted  22795 non-null  float64\n",
      " 16  Interest_Rate          12110 non-null  float64\n",
      " 17  Processing_Fee         11971 non-null  float64\n",
      " 18  EMI_Loan_Submitted     12110 non-null  float64\n",
      " 19  Filled_Form            37717 non-null  object \n",
      " 20  Device_Type            37717 non-null  object \n",
      " 21  Var2                   37717 non-null  object \n",
      " 22  Source                 37717 non-null  object \n",
      " 23  Var4                   37717 non-null  int64  \n",
      "dtypes: float64(8), int64(3), object(13)\n",
      "memory usage: 6.9+ MB\n"
     ]
    }
   ],
   "source": [
    "test = pd.read_csv('Test.csv',encoding='ISO-8859-1')\n",
    "test.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 124737 entries, 0 to 124736\n",
      "Data columns (total 27 columns):\n",
      " #   Column                 Non-Null Count   Dtype  \n",
      "---  ------                 --------------   -----  \n",
      " 0   ID                     124737 non-null  object \n",
      " 1   Gender                 124737 non-null  object \n",
      " 2   City                   123336 non-null  object \n",
      " 3   Monthly_Income         124737 non-null  int64  \n",
      " 4   DOB                    124737 non-null  object \n",
      " 5   Lead_Creation_Date     124737 non-null  object \n",
      " 6   Loan_Amount_Applied    124626 non-null  float64\n",
      " 7   Loan_Tenure_Applied    124626 non-null  float64\n",
      " 8   Existing_EMI           124626 non-null  float64\n",
      " 9   Employer_Name          124624 non-null  object \n",
      " 10  Salary_Account         107936 non-null  object \n",
      " 11  Mobile_Verified        124737 non-null  object \n",
      " 12  Var5                   124737 non-null  int64  \n",
      " 13  Var1                   124737 non-null  object \n",
      " 14  Loan_Amount_Submitted  75202 non-null   float64\n",
      " 15  Loan_Tenure_Submitted  75202 non-null   float64\n",
      " 16  Interest_Rate          39836 non-null   float64\n",
      " 17  Processing_Fee         39391 non-null   float64\n",
      " 18  EMI_Loan_Submitted     39836 non-null   float64\n",
      " 19  Filled_Form            124737 non-null  object \n",
      " 20  Device_Type            124737 non-null  object \n",
      " 21  Var2                   124737 non-null  object \n",
      " 22  Source                 124737 non-null  object \n",
      " 23  Var4                   124737 non-null  int64  \n",
      " 24  LoggedIn               87020 non-null   float64\n",
      " 25  Disbursed              87020 non-null   float64\n",
      " 26  source                 124737 non-null  object \n",
      "dtypes: float64(10), int64(3), object(14)\n",
      "memory usage: 25.7+ MB\n"
     ]
    }
   ],
   "source": [
    "#合成一个总的data，方便一起做特征工程\n",
    "train['source']= 'train'\n",
    "test['source'] = 'test'\n",
    "data = pd.concat([train, test],ignore_index=True)\n",
    "data.info()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 124737 entries, 0 to 124736\n",
      "Data columns (total 27 columns):\n",
      " #   Column                 Non-Null Count   Dtype  \n",
      "---  ------                 --------------   -----  \n",
      " 0   ID                     124737 non-null  object \n",
      " 1   Gender                 124737 non-null  object \n",
      " 2   City                   123336 non-null  object \n",
      " 3   Monthly_Income         124737 non-null  int64  \n",
      " 4   DOB                    124737 non-null  object \n",
      " 5   Lead_Creation_Date     124737 non-null  object \n",
      " 6   Loan_Amount_Applied    124626 non-null  float64\n",
      " 7   Loan_Tenure_Applied    124626 non-null  float64\n",
      " 8   Existing_EMI           124626 non-null  float64\n",
      " 9   Employer_Name          124624 non-null  object \n",
      " 10  Salary_Account         107936 non-null  object \n",
      " 11  Mobile_Verified        124737 non-null  object \n",
      " 12  Var5                   124737 non-null  int64  \n",
      " 13  Var1                   124737 non-null  object \n",
      " 14  Loan_Amount_Submitted  75202 non-null   float64\n",
      " 15  Loan_Tenure_Submitted  75202 non-null   float64\n",
      " 16  Interest_Rate          39836 non-null   float64\n",
      " 17  Processing_Fee         39391 non-null   float64\n",
      " 18  EMI_Loan_Submitted     39836 non-null   float64\n",
      " 19  Filled_Form            124737 non-null  object \n",
      " 20  Device_Type            124737 non-null  object \n",
      " 21  Var2                   124737 non-null  object \n",
      " 22  Source                 124737 non-null  object \n",
      " 23  Var4                   124737 non-null  int64  \n",
      " 24  LoggedIn               87020 non-null   float64\n",
      " 25  Disbursed              87020 non-null   float64\n",
      " 26  source                 124737 non-null  object \n",
      "dtypes: float64(10), int64(3), object(14)\n",
      "memory usage: 25.7+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Gender</th>\n",
       "      <th>City</th>\n",
       "      <th>Monthly_Income</th>\n",
       "      <th>DOB</th>\n",
       "      <th>Lead_Creation_Date</th>\n",
       "      <th>Loan_Amount_Applied</th>\n",
       "      <th>Loan_Tenure_Applied</th>\n",
       "      <th>Existing_EMI</th>\n",
       "      <th>Employer_Name</th>\n",
       "      <th>...</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Filled_Form</th>\n",
       "      <th>Device_Type</th>\n",
       "      <th>Var2</th>\n",
       "      <th>Source</th>\n",
       "      <th>Var4</th>\n",
       "      <th>LoggedIn</th>\n",
       "      <th>Disbursed</th>\n",
       "      <th>source</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ID000002C20</td>\n",
       "      <td>Female</td>\n",
       "      <td>Delhi</td>\n",
       "      <td>20000</td>\n",
       "      <td>23-May-78</td>\n",
       "      <td>15-May-15</td>\n",
       "      <td>300000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CYBOSOL</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ID000004E40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Mumbai</td>\n",
       "      <td>35000</td>\n",
       "      <td>07-Oct-85</td>\n",
       "      <td>04-May-15</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>TATA CONSULTANCY SERVICES LTD (TCS)</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6762.9</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ID000007H20</td>\n",
       "      <td>Male</td>\n",
       "      <td>Panchkula</td>\n",
       "      <td>22500</td>\n",
       "      <td>10-Oct-81</td>\n",
       "      <td>19-May-15</td>\n",
       "      <td>600000.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>ALCHEMIST HOSPITALS LTD</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ID000008I30</td>\n",
       "      <td>Male</td>\n",
       "      <td>Saharsa</td>\n",
       "      <td>35000</td>\n",
       "      <td>30-Nov-87</td>\n",
       "      <td>09-May-15</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>BIHAR GOVERNMENT</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ID000009J40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Bengaluru</td>\n",
       "      <td>100000</td>\n",
       "      <td>17-Feb-84</td>\n",
       "      <td>20-May-15</td>\n",
       "      <td>500000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>GLOBAL EDGE SOFTWARE</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S134</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ID  Gender       City  Monthly_Income        DOB  \\\n",
       "0  ID000002C20  Female      Delhi           20000  23-May-78   \n",
       "1  ID000004E40    Male     Mumbai           35000  07-Oct-85   \n",
       "2  ID000007H20    Male  Panchkula           22500  10-Oct-81   \n",
       "3  ID000008I30    Male    Saharsa           35000  30-Nov-87   \n",
       "4  ID000009J40    Male  Bengaluru          100000  17-Feb-84   \n",
       "\n",
       "  Lead_Creation_Date  Loan_Amount_Applied  Loan_Tenure_Applied  Existing_EMI  \\\n",
       "0          15-May-15             300000.0                  5.0           0.0   \n",
       "1          04-May-15             200000.0                  2.0           0.0   \n",
       "2          19-May-15             600000.0                  4.0           0.0   \n",
       "3          09-May-15            1000000.0                  5.0           0.0   \n",
       "4          20-May-15             500000.0                  2.0       25000.0   \n",
       "\n",
       "                         Employer_Name  ... Processing_Fee EMI_Loan_Submitted  \\\n",
       "0                              CYBOSOL  ...            NaN                NaN   \n",
       "1  TATA CONSULTANCY SERVICES LTD (TCS)  ...            NaN             6762.9   \n",
       "2              ALCHEMIST HOSPITALS LTD  ...            NaN                NaN   \n",
       "3                     BIHAR GOVERNMENT  ...            NaN                NaN   \n",
       "4                 GLOBAL EDGE SOFTWARE  ...            NaN                NaN   \n",
       "\n",
       "   Filled_Form  Device_Type  Var2  Source  Var4  LoggedIn  Disbursed source  \n",
       "0            N  Web-browser     G    S122     1       0.0        0.0  train  \n",
       "1            N  Web-browser     G    S122     3       0.0        0.0  train  \n",
       "2            N  Web-browser     B    S143     1       0.0        0.0  train  \n",
       "3            N  Web-browser     B    S143     3       0.0        0.0  train  \n",
       "4            N  Web-browser     B    S134     3       1.0        0.0  train  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZEAAAEGCAYAAACkQqisAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAVt0lEQVR4nO3df7AlZX3n8fdn+CGIIKCDRQbIYJyoKP4cEU02RjA46AaQkgTLFUrZJUmB4ia6wSQlUSRKAhhJ/LFUIIDrgsiKIqKziIAbV5Hhh+KAFBOMMAuBMSAiLLAD3/3jPBeOM/fe6emZc+893Per6tR0P/10n++ZGv3Q/XQ/napCkqQ+Fsx2AZKk8WWISJJ6M0QkSb0ZIpKk3gwRSVJvW852ATPt2c9+di1evHi2y5CksXHttdf+tKoWTrZt3oXI4sWLWbFixWyXIUljI8lPptrm5SxJUm+GiCSpN0NEktSbISJJ6s0QkST1ZohIknozRCRJvRkikqTeDBFJUm/z7on1TfXK95872yVoDrr2b46Y7RKkWeGZiCSpN0NEktSbISJJ6s0QkST1ZohIknozRCRJvRkikqTeDBFJUm+GiCSpt5GGSJL/nGRl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N4Dnz8Tfi+Yfz0SknqrqthYEu6yz6X3AMVX17STPAB5u7fsweP/KT4CvA4cCF27ga34deENVPZbkK+seN8kBwJJ27AAXJ/kt4EEG09G8nMH/zq8Drt20XyytzzMRadNMNqPxt4HTkrwH2HHoMtT32jtYHgPOA36zw/G/0PpPddwD2ud6BkHxAgah8u+Ai6rqoar6Oc5tphExRKSekjwXeAy4Z7i9qj4G/EdgW+C7SV4wsWmdQ9Qk7dus0+fBDRw3wEer6mXt87yqOnOK75M2O0NE6iHJQuAzwN/XOhPQJfm1qrqxqk5mMNg+ESL7tJmPFwC/D/xTa787yQtb+1um+c7JjrsceFe7vEWSRW3Q/1vAW5Js28ZOfndz/XZpmGMiUnfbJrkB2ApYC3wWOG2Sfu9N8noGZyk3AV8DXsNgZtmPAXsz+D/5i1r/44FLGLwt8ofAM6b4/vWOW1WPJHkh8J0kAL8A/kNVXZfk88ANDMZg/tem/HBpKs7iK0nqzctZkqTeDBFJUm+GiCSpN0NEktSbISJJ6s0QkST1ZohIknr7/76DOJqiBNyRAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Target 分布，两类样本分布严重不均衡，只有1.4%的样本Disbursed为1\n",
    "sns.countplot(train['Disbursed']);\n",
    "plt.xlabel('Disbursed');\n",
    "plt.ylabel('Number of occurrences');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID                           0\n",
      "Gender                       0\n",
      "City                      1401\n",
      "Monthly_Income               0\n",
      "DOB                          0\n",
      "Lead_Creation_Date           0\n",
      "Loan_Amount_Applied        111\n",
      "Loan_Tenure_Applied        111\n",
      "Existing_EMI               111\n",
      "Employer_Name              113\n",
      "Salary_Account           16801\n",
      "Mobile_Verified              0\n",
      "Var5                         0\n",
      "Var1                         0\n",
      "Loan_Amount_Submitted    49535\n",
      "Loan_Tenure_Submitted    49535\n",
      "Interest_Rate            84901\n",
      "Processing_Fee           85346\n",
      "EMI_Loan_Submitted       84901\n",
      "Filled_Form                  0\n",
      "Device_Type                  0\n",
      "Var2                         0\n",
      "Source                       0\n",
      "Var4                         0\n",
      "LoggedIn                 37717\n",
      "Disbursed                37717\n",
      "source                       0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(data.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 连续型特征用中值代替缺失值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loan Tenure "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#不合理的贷款年限，设为缺失值\n",
    "data['Loan_Tenure_Applied'].replace([10,6,7,8,9],value = np.nan, inplace = True)\n",
    "data['Loan_Tenure_Submitted'].replace(6, np.nan, inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID                           0\n",
      "Gender                       0\n",
      "City                      1401\n",
      "Monthly_Income               0\n",
      "DOB                          0\n",
      "Lead_Creation_Date           0\n",
      "Loan_Amount_Applied          0\n",
      "Loan_Tenure_Applied          0\n",
      "Existing_EMI                 0\n",
      "Employer_Name              113\n",
      "Salary_Account           16801\n",
      "Mobile_Verified              0\n",
      "Var5                         0\n",
      "Var1                         0\n",
      "Loan_Amount_Submitted        0\n",
      "Loan_Tenure_Submitted        0\n",
      "Interest_Rate                0\n",
      "Processing_Fee               0\n",
      "EMI_Loan_Submitted           0\n",
      "Filled_Form                  0\n",
      "Device_Type                  0\n",
      "Var2                         0\n",
      "Source                       0\n",
      "Var4                         0\n",
      "LoggedIn                     0\n",
      "Disbursed                    0\n",
      "source                       0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "medians = data.median() \n",
    "data = data.fillna(medians)\n",
    "\n",
    "print(data.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类别型特征编码\n",
    "### 对类别型特征进行独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gender属性的不同取值和出现的次数\n",
      "Male      49848\n",
      "Female    37172\n",
      "Name: Gender, dtype: int64\n",
      "\n",
      "City属性的不同取值和出现的次数\n",
      "Delhi        12527\n",
      "Bengaluru    10824\n",
      "Mumbai       10795\n",
      "Hyderabad     7272\n",
      "Chennai       6916\n",
      "             ...  \n",
      "Kupwara          1\n",
      "Dantewada        1\n",
      "KHAMBHAT         1\n",
      "Seoni            1\n",
      "Madhepura        1\n",
      "Name: City, Length: 697, dtype: int64\n",
      "\n",
      "Employer_Name属性的不同取值和出现的次数\n",
      "0                                               4914\n",
      "TATA CONSULTANCY SERVICES LTD (TCS)              550\n",
      "COGNIZANT TECHNOLOGY SOLUTIONS INDIA PVT LTD     404\n",
      "ACCENTURE SERVICES PVT LTD                       324\n",
      "GOOGLE                                           301\n",
      "                                                ... \n",
      "AGILE SOFTWARE CORPORATION                         1\n",
      "CAPGEMINI CONSULTING IND...                        1\n",
      "ANJUL SINGH CHAUHAN                                1\n",
      "SAGAR KAMBLE                                       1\n",
      "RANBAXY EMPLOYEES WELFARE SOCIETY                  1\n",
      "Name: Employer_Name, Length: 43567, dtype: int64\n",
      "\n",
      "Salary_Account属性的不同取值和出现的次数\n",
      "HDFC Bank                                          17695\n",
      "ICICI Bank                                         13636\n",
      "State Bank of India                                11843\n",
      "Axis Bank                                           8783\n",
      "Citibank                                            2376\n",
      "Kotak Bank                                          2067\n",
      "IDBI Bank                                           1550\n",
      "Punjab National Bank                                1201\n",
      "Bank of India                                       1170\n",
      "Bank of Baroda                                      1126\n",
      "Standard Chartered Bank                              995\n",
      "Canara Bank                                          990\n",
      "Union Bank of India                                  951\n",
      "Yes Bank                                             779\n",
      "ING Vysya                                            678\n",
      "Corporation bank                                     649\n",
      "Indian Overseas Bank                                 612\n",
      "State Bank of Hyderabad                              597\n",
      "Indian Bank                                          555\n",
      "Oriental Bank of Commerce                            524\n",
      "IndusInd Bank                                        503\n",
      "Andhra Bank                                          485\n",
      "Central Bank of India                                445\n",
      "Syndicate Bank                                       415\n",
      "Bank of Maharasthra                                  406\n",
      "State Bank of Bikaner & Jaipur                       331\n",
      "HSBC                                                 328\n",
      "Karur Vysya Bank                                     326\n",
      "State Bank of Mysore                                 255\n",
      "Federal Bank                                         253\n",
      "Vijaya Bank                                          252\n",
      "Allahabad Bank                                       238\n",
      "UCO Bank                                             237\n",
      "State Bank of Travancore                             227\n",
      "Karnataka Bank                                       200\n",
      "Saraswat Bank                                        195\n",
      "United Bank of India                                 183\n",
      "Dena Bank                                            182\n",
      "State Bank of Patiala                                177\n",
      "South Indian Bank                                    160\n",
      "Deutsche Bank                                        125\n",
      "Abhyuday Co-op Bank Ltd                              108\n",
      "The Ratnakar Bank Ltd                                 83\n",
      "Tamil Nadu Mercantile Bank                            71\n",
      "Punjab & Sind bank                                    66\n",
      "J&K Bank                                              59\n",
      "Lakshmi Vilas bank                                    50\n",
      "Dhanalakshmi Bank Ltd                                 42\n",
      "State Bank of Indore                                  18\n",
      "Catholic Syrian Bank                                  14\n",
      "India Bulls                                           11\n",
      "B N P Paribas                                          8\n",
      "GIC Housing Finance Ltd                                8\n",
      "Firstrand Bank Limited                                 7\n",
      "Bank of Rajasthan                                      5\n",
      "Kerala Gramin Bank                                     4\n",
      "Industrial And Commercial Bank Of China Limited        2\n",
      "Name: Salary_Account, dtype: int64\n",
      "\n",
      "Mobile_Verified属性的不同取值和出现的次数\n",
      "Y    56481\n",
      "N    30539\n",
      "Name: Mobile_Verified, dtype: int64\n",
      "\n",
      "Var1属性的不同取值和出现的次数\n",
      "HBXX    59294\n",
      "HBXC     9010\n",
      "HBXB     4479\n",
      "HAXA     2909\n",
      "HBXA     2123\n",
      "HAXB     2011\n",
      "HBXD     1964\n",
      "HAXC     1536\n",
      "HBXH      970\n",
      "HCXF      722\n",
      "HAYT      508\n",
      "HAVC      384\n",
      "HAXM      268\n",
      "HCXD      237\n",
      "HCYS      217\n",
      "HVYS      186\n",
      "HAZD      109\n",
      "HCXG       78\n",
      "HAXF       15\n",
      "Name: Var1, dtype: int64\n",
      "\n",
      "Var2属性的不同取值和出现的次数\n",
      "B    37280\n",
      "G    33032\n",
      "C    14210\n",
      "E     1315\n",
      "D      634\n",
      "F      544\n",
      "A        5\n",
      "Name: Var2, dtype: int64\n",
      "\n",
      "Filled_Form属性的不同取值和出现的次数\n",
      "N    67530\n",
      "Y    19490\n",
      "Name: Filled_Form, dtype: int64\n",
      "\n",
      "Device_Type属性的不同取值和出现的次数\n",
      "Web-browser    64316\n",
      "Mobile         22704\n",
      "Name: Device_Type, dtype: int64\n",
      "\n",
      "Source属性的不同取值和出现的次数\n",
      "S122    38567\n",
      "S133    29885\n",
      "S159     5599\n",
      "S143     4332\n",
      "S127     1931\n",
      "S137     1724\n",
      "S134     1301\n",
      "S161      769\n",
      "S151      720\n",
      "S157      650\n",
      "S153      494\n",
      "S156      308\n",
      "S144      299\n",
      "S158      208\n",
      "S123       73\n",
      "S141       57\n",
      "S162       36\n",
      "S124       24\n",
      "S160       11\n",
      "S150       10\n",
      "S155        4\n",
      "S139        3\n",
      "S136        3\n",
      "S129        3\n",
      "S138        3\n",
      "S135        2\n",
      "S130        1\n",
      "S125        1\n",
      "S154        1\n",
      "S140        1\n",
      "Name: Source, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#对类别型特征，观察其取值范围及直方图\n",
    "#对类别型特征，观察其取值范围\n",
    "categorical_features = ['Gender','City','Employer_Name','Salary_Account','Mobile_Verified','Var1','Var2','Filled_Form','Device_Type','Source']\n",
    "for col in categorical_features:\n",
    "    print ('\\n%s属性的不同取值和出现的次数'%col)\n",
    "    print (train[col].value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### City、Employer_Name、Salary_Account、Source\n",
    "这些特征都是取值很多,\n",
    "取前几个重要的，其余合并成一个：others\n",
    "\n",
    "LightGBM对类别特征建立直方图时，当特征取值数目超过max_bin(默认255)，会去掉样本数目少的类别：\n",
    "统计该特征下每一种离散值出现的次数，并从高到低排序，并过滤掉出现次数较少的特征值, \n",
    "然后为每一个特征值，建立一个bin容器, 对于在bin容器内出现次数较少的特征值直接过滤掉，不建立bin容器。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_features = ['City','Employer_Name','Salary_Account', 'Source']\n",
    "rare_thresholds = [100, 30, 40, 40]\n",
    "j=0\n",
    "for col in cat_features:\n",
    "    #每个取值的样本数目\n",
    "    value_counts_col =  data[col].value_counts(dropna=False) #保留 nan\n",
    "\n",
    "    #样本数目小于阈值的取值为稀有取值\n",
    "    rare_threshold = rare_thresholds[j]\n",
    "    value_counts_rare = list(value_counts_col[value_counts_col < rare_threshold ].index)\n",
    "\n",
    "    #稀有值合并为：others\n",
    "    rare_index = data[col].isin(value_counts_rare)\n",
    "    data.loc[ data[col].isin(value_counts_rare), col] = \"Others\"\n",
    "    \n",
    "    j = j+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Gender属性有2的不同取值，各取值及其出现的次数\n",
      "\n",
      "Male      71398\n",
      "Female    53339\n",
      "Name: Gender, dtype: int64\n",
      "\n",
      "City属性有76的不同取值，各取值及其出现的次数\n",
      "\n",
      "Delhi          17936\n",
      "Bengaluru      15522\n",
      "Mumbai         15425\n",
      "Others         11574\n",
      "Hyderabad      10410\n",
      "               ...  \n",
      "Thrissur         112\n",
      "Gandhinagar      111\n",
      "Panaji           110\n",
      "Rewari           110\n",
      "Vellore          102\n",
      "Name: City, Length: 75, dtype: int64\n",
      "\n",
      "Employer_Name属性有263的不同取值，各取值及其出现的次数\n",
      "\n",
      "Others                                          98390\n",
      "0                                                6900\n",
      "TATA CONSULTANCY SERVICES LTD (TCS)               754\n",
      "COGNIZANT TECHNOLOGY SOLUTIONS INDIA PVT LTD      558\n",
      "ACCENTURE SERVICES PVT LTD                        476\n",
      "                                                ...  \n",
      "AMDOCS DEVELOPMENT CENTRE INDIA PVT LTD            30\n",
      "SERENDIPITY INFOLABS PVT LTD                       30\n",
      "SOCIETE GENERALE                                   30\n",
      "PANTALOONS FASHION AND RETAIL LTD                  30\n",
      "FLIPKART INDIA PVT LTD                             30\n",
      "Name: Employer_Name, Length: 262, dtype: int64\n",
      "\n",
      "Salary_Account属性有50的不同取值，各取值及其出现的次数\n",
      "\n",
      "HDFC Bank                         25180\n",
      "ICICI Bank                        19547\n",
      "State Bank of India               17110\n",
      "Axis Bank                         12590\n",
      "Citibank                           3398\n",
      "Kotak Bank                         2955\n",
      "IDBI Bank                          2213\n",
      "Punjab National Bank               1747\n",
      "Bank of India                      1713\n",
      "Bank of Baroda                     1675\n",
      "Standard Chartered Bank            1434\n",
      "Canara Bank                        1385\n",
      "Union Bank of India                1330\n",
      "Yes Bank                           1120\n",
      "ING Vysya                           996\n",
      "Corporation bank                    948\n",
      "Indian Overseas Bank                901\n",
      "State Bank of Hyderabad             854\n",
      "Indian Bank                         773\n",
      "Oriental Bank of Commerce           761\n",
      "IndusInd Bank                       711\n",
      "Andhra Bank                         706\n",
      "Central Bank of India               648\n",
      "Syndicate Bank                      614\n",
      "Bank of Maharasthra                 576\n",
      "HSBC                                474\n",
      "State Bank of Bikaner & Jaipur      448\n",
      "Karur Vysya Bank                    435\n",
      "State Bank of Mysore                385\n",
      "Federal Bank                        377\n",
      "Vijaya Bank                         354\n",
      "Allahabad Bank                      345\n",
      "UCO Bank                            344\n",
      "State Bank of Travancore            333\n",
      "Karnataka Bank                      279\n",
      "United Bank of India                276\n",
      "Dena Bank                           268\n",
      "Saraswat Bank                       265\n",
      "State Bank of Patiala               263\n",
      "South Indian Bank                   223\n",
      "Deutsche Bank                       176\n",
      "Abhyuday Co-op Bank Ltd             161\n",
      "Others                              132\n",
      "The Ratnakar Bank Ltd               113\n",
      "Tamil Nadu Mercantile Bank          103\n",
      "Punjab & Sind bank                   84\n",
      "J&K Bank                             78\n",
      "Lakshmi Vilas bank                   69\n",
      "Dhanalakshmi Bank Ltd                66\n",
      "Name: Salary_Account, dtype: int64\n",
      "\n",
      "Mobile_Verified属性有2的不同取值，各取值及其出现的次数\n",
      "\n",
      "Y    80928\n",
      "N    43809\n",
      "Name: Mobile_Verified, dtype: int64\n",
      "\n",
      "Var1属性有19的不同取值，各取值及其出现的次数\n",
      "\n",
      "HBXX    84901\n",
      "HBXC    12952\n",
      "HBXB     6502\n",
      "HAXA     4214\n",
      "HBXA     3042\n",
      "HAXB     2879\n",
      "HBXD     2818\n",
      "HAXC     2171\n",
      "HBXH     1387\n",
      "HCXF      990\n",
      "HAYT      710\n",
      "HAVC      570\n",
      "HAXM      386\n",
      "HCXD      348\n",
      "HCYS      318\n",
      "HVYS      252\n",
      "HAZD      161\n",
      "HCXG      114\n",
      "HAXF       22\n",
      "Name: Var1, dtype: int64\n",
      "\n",
      "Filled_Form属性有2的不同取值，各取值及其出现的次数\n",
      "\n",
      "N    96740\n",
      "Y    27997\n",
      "Name: Filled_Form, dtype: int64\n",
      "\n",
      "Device_Type属性有2的不同取值，各取值及其出现的次数\n",
      "\n",
      "Web-browser    92105\n",
      "Mobile         32632\n",
      "Name: Device_Type, dtype: int64\n",
      "\n",
      "Var2属性有7的不同取值，各取值及其出现的次数\n",
      "\n",
      "B    53481\n",
      "G    47338\n",
      "C    20366\n",
      "E     1855\n",
      "D      918\n",
      "F      770\n",
      "A        9\n",
      "Name: Var2, dtype: int64\n",
      "\n",
      "Source属性有19的不同取值，各取值及其出现的次数\n",
      "\n",
      "S122      55249\n",
      "S133      42900\n",
      "S159       7999\n",
      "S143       6140\n",
      "S127       2804\n",
      "S137       2450\n",
      "S134       1900\n",
      "S161       1109\n",
      "S151       1018\n",
      "S157        929\n",
      "S153        705\n",
      "S144        447\n",
      "S156        432\n",
      "S158        294\n",
      "S123        112\n",
      "S141         83\n",
      "Others       63\n",
      "S162         60\n",
      "S124         43\n",
      "Name: Source, dtype: int64\n",
      "\n",
      "Var4属性有8的不同取值，各取值及其出现的次数\n",
      "\n",
      "3    36280\n",
      "1    34316\n",
      "5    29092\n",
      "4     9411\n",
      "2     8481\n",
      "0     3564\n",
      "7     3264\n",
      "6      329\n",
      "Name: Var4, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "cat_features = ['Gender','City','Employer_Name','Salary_Account','Mobile_Verified','Var1','Filled_Form','Device_Type','Var2','Source','Var4']\n",
    "for col in cat_features:\n",
    "    num_vlaules = len(data[col].unique())\n",
    "    print ('\\n%s属性有%d的不同取值，各取值及其出现的次数\\n'% (col,num_vlaules) )\n",
    "    print (data[col].value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DOB\n",
    "DOB是出生的具体日期，具体日期可能没作用，转换成年龄(申请贷款的年龄)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    37\n",
       "1    30\n",
       "2    34\n",
       "3    28\n",
       "4    31\n",
       "Name: Age, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建一个年龄的字段Age\n",
    "data['Age'] = pd.to_datetime(data['Lead_Creation_Date']).dt.year - pd.to_datetime(data['DOB']).dt.year\n",
    "data['Age'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#把原始的DOB字段去掉:\n",
    "data.drop(['DOB', 'Lead_Creation_Date'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LoggedIn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#不能用于预测特征，drop\n",
    "data.drop('LoggedIn',axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类别特征先编码成数值，LightGBM无需One-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "le = LabelEncoder()\n",
    "feats_to_encode = ['City', 'Employer_Name', 'Salary_Account']\n",
    "for col in feats_to_encode:\n",
    "    data[col] = data[col].astype('str')\n",
    "    data[col] = le.fit_transform(data[col])\n",
    "    \n",
    "feats_to_encode = ['Device_Type','Filled_Form','Gender','Mobile_Verified','Source','Var1','Var2','Var4']\n",
    "for col in feats_to_encode:\n",
    "    data[col] = le.fit_transform(data[col])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Gender</th>\n",
       "      <th>City</th>\n",
       "      <th>Monthly_Income</th>\n",
       "      <th>Loan_Amount_Applied</th>\n",
       "      <th>Loan_Tenure_Applied</th>\n",
       "      <th>Existing_EMI</th>\n",
       "      <th>Employer_Name</th>\n",
       "      <th>Salary_Account</th>\n",
       "      <th>Mobile_Verified</th>\n",
       "      <th>...</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Filled_Form</th>\n",
       "      <th>Device_Type</th>\n",
       "      <th>Var2</th>\n",
       "      <th>Source</th>\n",
       "      <th>Var4</th>\n",
       "      <th>Disbursed</th>\n",
       "      <th>source</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ID000002C20</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>20000</td>\n",
       "      <td>300000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>192</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>9409.23</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ID000004E40</td>\n",
       "      <td>1</td>\n",
       "      <td>44</td>\n",
       "      <td>35000</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>227</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>6762.90</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ID000007H20</td>\n",
       "      <td>1</td>\n",
       "      <td>52</td>\n",
       "      <td>22500</td>\n",
       "      <td>600000.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>192</td>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>9409.23</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ID000008I30</td>\n",
       "      <td>1</td>\n",
       "      <td>52</td>\n",
       "      <td>35000</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>192</td>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>9409.23</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ID000009J40</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>100000</td>\n",
       "      <td>500000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>192</td>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3900.0</td>\n",
       "      <td>9409.23</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>train</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ID  Gender  City  Monthly_Income  Loan_Amount_Applied  \\\n",
       "0  ID000002C20       0    15           20000             300000.0   \n",
       "1  ID000004E40       1    44           35000             200000.0   \n",
       "2  ID000007H20       1    52           22500             600000.0   \n",
       "3  ID000008I30       1    52           35000            1000000.0   \n",
       "4  ID000009J40       1     6          100000             500000.0   \n",
       "\n",
       "   Loan_Tenure_Applied  Existing_EMI  Employer_Name  Salary_Account  \\\n",
       "0                  5.0           0.0            192              15   \n",
       "1                  2.0           0.0            227              17   \n",
       "2                  4.0           0.0            192              37   \n",
       "3                  5.0           0.0            192              37   \n",
       "4                  2.0       25000.0            192              15   \n",
       "\n",
       "   Mobile_Verified  ...  Processing_Fee  EMI_Loan_Submitted  Filled_Form  \\\n",
       "0                0  ...          3900.0             9409.23            0   \n",
       "1                1  ...          3900.0             6762.90            0   \n",
       "2                1  ...          3900.0             9409.23            0   \n",
       "3                1  ...          3900.0             9409.23            0   \n",
       "4                1  ...          3900.0             9409.23            0   \n",
       "\n",
       "   Device_Type  Var2  Source  Var4  Disbursed  source  Age  \n",
       "0            1     6       1     1        0.0   train   37  \n",
       "1            1     6       1     3        0.0   train   30  \n",
       "2            1     1       9     1        0.0   train   34  \n",
       "3            1     1       9     3        0.0   train   28  \n",
       "4            1     1       6     3        0.0   train   31  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 124737 entries, 0 to 124736\n",
      "Data columns (total 25 columns):\n",
      " #   Column                 Non-Null Count   Dtype  \n",
      "---  ------                 --------------   -----  \n",
      " 0   ID                     124737 non-null  object \n",
      " 1   Gender                 124737 non-null  int32  \n",
      " 2   City                   124737 non-null  int32  \n",
      " 3   Monthly_Income         124737 non-null  int64  \n",
      " 4   Loan_Amount_Applied    124737 non-null  float64\n",
      " 5   Loan_Tenure_Applied    124737 non-null  float64\n",
      " 6   Existing_EMI           124737 non-null  float64\n",
      " 7   Employer_Name          124737 non-null  int32  \n",
      " 8   Salary_Account         124737 non-null  int32  \n",
      " 9   Mobile_Verified        124737 non-null  int32  \n",
      " 10  Var5                   124737 non-null  int64  \n",
      " 11  Var1                   124737 non-null  int32  \n",
      " 12  Loan_Amount_Submitted  124737 non-null  float64\n",
      " 13  Loan_Tenure_Submitted  124737 non-null  float64\n",
      " 14  Interest_Rate          124737 non-null  float64\n",
      " 15  Processing_Fee         124737 non-null  float64\n",
      " 16  EMI_Loan_Submitted     124737 non-null  float64\n",
      " 17  Filled_Form            124737 non-null  int32  \n",
      " 18  Device_Type            124737 non-null  int32  \n",
      " 19  Var2                   124737 non-null  int32  \n",
      " 20  Source                 124737 non-null  int32  \n",
      " 21  Var4                   124737 non-null  int64  \n",
      " 22  Disbursed              124737 non-null  float64\n",
      " 23  source                 124737 non-null  object \n",
      " 24  Age                    124737 non-null  int64  \n",
      "dtypes: float64(9), int32(10), int64(4), object(2)\n",
      "memory usage: 19.0+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID                       0\n",
      "Gender                   0\n",
      "City                     0\n",
      "Monthly_Income           0\n",
      "Loan_Amount_Applied      0\n",
      "Loan_Tenure_Applied      0\n",
      "Existing_EMI             0\n",
      "Employer_Name            0\n",
      "Salary_Account           0\n",
      "Mobile_Verified          0\n",
      "Var5                     0\n",
      "Var1                     0\n",
      "Loan_Amount_Submitted    0\n",
      "Loan_Tenure_Submitted    0\n",
      "Interest_Rate            0\n",
      "Processing_Fee           0\n",
      "EMI_Loan_Submitted       0\n",
      "Filled_Form              0\n",
      "Device_Type              0\n",
      "Var2                     0\n",
      "Source                   0\n",
      "Var4                     0\n",
      "Disbursed                0\n",
      "source                   0\n",
      "Age                      0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(data.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 区分训练和测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = data.loc[data['source']=='train']\n",
    "test = data.loc[data['source']=='test']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\anaconda3\\envs\\tf1.14\\lib\\site-packages\\pandas\\core\\frame.py:3997: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  errors=errors,\n"
     ]
    }
   ],
   "source": [
    "train.drop('source',axis=1,inplace=True)\n",
    "test.drop(['source','Disbursed'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.to_csv('FE_train.csv',index=False)\n",
    "test.to_csv('FE_test.csv',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
